##!/bin/bash
#MY_LIBS_NAME=FFmpeg-n4.0.1
#MY_DIR=FFmpeg-n4.0.1
#
## cd ./${MY_DIR}
#
##编译的过程中产生的中间件的存放目录，为了区分编译目录，源码目录，install目录
#MY_BUILD_DIR=binary
#
#
#NDK_PATH=/Users/KevenTao/Documents/work_soft/AndroidStudio/android-ndk-r14b
#BUILD_PLATFORM=darwin-x86_64
#TOOLCHAIN_VERSION=4.9
#ANDROID_VERSION=24
#
#ANDROID_ARMV5_CFLAGS="-march=armv5te"
#ANDROID_ARMV7_CFLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=neon"  #-mfloat-abi=hard -mfpu=vfpv3-d16 #-mfloat-abi=hard -mfpu=vfp
#ANDROID_ARMV8_CFLAGS="-march=armv8-a"
#ANDROID_X86_CFLAGS="-march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32"
#ANDROID_X86_64_CFLAGS="-march=x86-64 -msse4.2 -mpopcnt -m64 -mtune=intel"
#
#
## params($1:arch,$2:arch_abi,$3:host,$4:cross_prefix,$5:cflags)
#build_bin() {
#
#echo "-------------------star build $2-------------------------"
#
#ARCH=$1         # arm arm64 x86 x86_64
#ANDROID_ARCH_ABI=$2     # armeabi armeabi-v7a x86 mips
#
#PREFIX=$(pwd)/dist/${MY_LIBS_NAME}/${ANDROID_ARCH_ABI}/
#
#HOST=$3
#SYSROOT=${NDK_PATH}/platforms/android-${ANDROID_VERSION}/arch-${ARCH}
#
#CFALGS=$5
#
#
#TOOLCHAIN=${NDK_PATH}/toolchains/${HOST}-${TOOLCHAIN_VERSION}/prebuilt/${BUILD_PLATFORM}
#CROSS_PREFIX=${TOOLCHAIN}/bin/$4-
#
## build 中间件
#BUILD_DIR=./${MY_BUILD_DIR}/${ANDROID_ARCH_ABI}
#
#echo "pwd==$(pwd)"
#echo "ARCH==${ARCH}"
#echo "PREFIX==${PREFIX}"
#echo "HOST==${HOST}"
#echo "SYSROOT=${SYSROOT}"
#echo "CFALGS=$5"
#echo "CFALGS=${CFALGS}"
#echo "TOOLCHAIN==${TOOLCHAIN}"
#echo "CROSS_PREFIX=${CROSS_PREFIX}"
#
##echo "-------------------------按任意键继续---------------------"
##read -n 1
##echo "-------------------------继续执行-------------------------"
#
## mkdir -p ${BUILD_DIR}   #创建当前arch_abi的编译目录,比如:binary/armeabi-v7a
## cd ${BUILD_DIR}         #此处 进了当前arch_abi的2级编译目录
#
#
#bash ./configure \
#--prefix=${PREFIX} \
#--target-os=linux \
#--arch=${ARCH} \
#--sysroot=$SYSROOT \
#--enable-cross-compile \
#--cross-prefix=${CROSS_PREFIX} \
#--extra-cflags="$CFALGS -Os -fPIC -DANDROID -Wfatal-errors -Wno-deprecated" \
#--extra-cxxflags="-D__thumb__ -fexceptions -frtti" \
#--extra-ldflags="-L${SYSROOT}/usr/lib" \
#--disable-shared \
#--enable-asm \
#--enable-neon \
#--disable-encoders \
#--enable-encoder=aac \
#--enable-encoder=mjpeg \
#--enable-encoder=png \
#--disable-decoders \
#--enable-decoder=aac \
#--enable-decoder=aac_latm \
#--enable-decoder=h264 \
#--enable-decoder=mpeg4 \
#--enable-decoder=mjpeg \
#--enable-decoder=png \
#--disable-demuxers \
#--enable-demuxer=image2 \
#--enable-demuxer=h264 \
#--enable-demuxer=aac \
#--disable-parsers \
#--enable-parser=aac \
#--enable-parser=ac3 \
#--enable-parser=h264 \
#--enable-gpl \
#--disable-doc \
#--disable-ffmpeg \
#--disable-ffplay \
#--disable-ffprobe \
#--disable-symver \
#--disable-debug \
#--enable-small
#
#
#make clean
#make
#make install
#
##从当前arch_abi编译目录跳出，对应上面的cd ${BUILD_DIR},以便function多次执行
#cd ../../
#
#echo "-------------------$2 build end-------------------------"
#}
#
#
## build armeabi
## build_bin arm armeabi arm-linux-androideabi arm-linux-androideabi "$ANDROID_ARMV5_CFLAGS"
#
##build armeabi-v7a
## build_bin arm armeabi-v7a arm-linux-androideabi arm-linux-androideabi "$ANDROID_ARMV7_CFLAGS"
#
##build arm64-v8a
#build_bin arm64 arm64-v8a aarch64-linux-android aarch64-linux-android "$ANDROID_ARMV8_CFLAGS"
#
##build x86
## build_bin x86 x86 x86 i686-linux-android "$ANDROID_X86_CFLAGS"
#
##build x86_64
## build_bin x86_64 x86_64 x86_64 x86_64-linux-android "$ANDROID_X86_64_CFLAGS"

#!/bin/bash
export TMPDIR=/Users/KevenTao/Downloads/ffmpeg-3.3-2.9/ffmpegtemp #这句很重要，不然会报错 unable to create temporary file in

# NDK的路径，根据自己的安装位置进行设置
NDK=/Users/KevenTao/Documents/work_soft/AndroidStudio/android-ndk-r14b

# 编译针对的平台，可以根据自己的需求进行设置
# 这里选择最低支持android-14, arm架构，生成的so库是放在
# libs/armeabi文件夹下的，若针对x86架构，要选择arch-x86
PLATFORM=$NDK/platforms/android-24/arch-arm64

# 工具链的路径，根据编译的平台不同而不同
# aarch64-linux-android-4.9与上面设置的PLATFORM对应，4.9为工具的版本号，
# 根据自己安装的NDK版本来确定，一般使用最新的版本
TOOLCHAIN=$NDK/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64

function build_one
{
./configure \
--prefix=$PREFIX \
--target-os=linux \
--cross-prefix=$TOOLCHAIN/bin/aarch64-linux-android- \
--arch=arm64 \
--sysroot=$PLATFORM \
--extra-cflags="-I$PLATFORM/usr/include" \
--cc=$TOOLCHAIN/bin/aarch64-linux-android-gcc \
--nm=$TOOLCHAIN/bin/aarch64-linux-android-nm \
--disable-shared \
--enable-runtime-cpudetect \
--enable-gpl \
--enable-small \
--enable-cross-compile \
--disable-debug \
--enable-static \
--disable-doc \
--disable-asm \
--disable-ffmpeg \
--disable-ffplay \
--disable-ffprobe \
--disable-ffserver \
--disable-postproc \
--disable-avdevice \
--disable-symver \
--disable-stripping \

$ADDITIONAL_CONFIGURE_FLAG
sed -i '' 's/HAVE_LRINT 0/HAVE_LRINT 1/g' config.h
sed -i '' 's/HAVE_LRINTF 0/HAVE_LRINTF 1/g' config.h
sed -i '' 's/HAVE_ROUND 0/HAVE_ROUND 1/g' config.h
sed -i '' 's/HAVE_ROUNDF 0/HAVE_ROUNDF 1/g' config.h
sed -i '' 's/HAVE_TRUNC 0/HAVE_TRUNC 1/g' config.h
sed -i '' 's/HAVE_TRUNCF 0/HAVE_TRUNCF 1/g' config.h
sed -i '' 's/HAVE_CBRT 0/HAVE_CBRT 1/g' config.h
sed -i '' 's/HAVE_RINT 0/HAVE_RINT 1/g' config.h

make clean
make -j4
make install

$TOOLCHAIN/bin/aarch64-linux-android-ld \
-rpath-link=$PLATFORM/usr/lib \
-L$PLATFORM/usr/lib \
-L$PREFIX/lib \
-soname libffmpeg.so -shared -nostdlib -Bsymbolic --whole-archive --no-undefined -o \
$PREFIX/libffmpeg.so \
libavcodec/libavcodec.a \
libavfilter/libavfilter.a \
libswresample/libswresample.a \
libavformat/libavformat.a \
libavutil/libavutil.a \
libswscale/libswscale.a \
-lc -lm -lz -ldl -llog --dynamic-linker=/system/bin/linker \
$TOOLCHAIN/lib/gcc/aarch64-linux-android/4.9.x/libgcc.a \

}

# arm v8vfp
CPU=arm64-v8a
OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU "
PREFIX=./android/$CPU-vfp
ADDITIONAL_CONFIGURE_FLAG=
build_one

# CPU=armv
# PREFIX=$(pwd)/android/$CPU
# ADDI_CFLAGS="-marm"
# build_one

#arm v6
#CPU=armv6
#OPTIMIZE_CFLAGS="-marm -march=$CPU"
#PREFIX=./android/$CPU
#ADDITIONAL_CONFIGURE_FLAG=
#build_one

#arm v7vfpv3
# CPU=armv7-a
# OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfpv3-d16 -marm -march=$CPU "
# PREFIX=./android/$CPU
# ADDITIONAL_CONFIGURE_FLAG=
# build_one

#arm v7n
#CPU=armv7-a
#OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=neon -marm -march=$CPU -mtune=cortex-a8"
#PREFIX=./android/$CPU
#ADDITIONAL_CONFIGURE_FLAG=--enable-neon
#build_one

#arm v6+vfp
#CPU=armv6
#OPTIMIZE_CFLAGS="-DCMP_HAVE_VFP -mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU"
#PREFIX=./android/${CPU}_vfp
#ADDITIONAL_CONFIGURE_FLAG=
#build_one
